package com.ycsj.goldmedalnewconcept.master.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.github.mikephil.charting.charts.LineChart;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.manager.PickerManager;
import com.ycsj.common.utils.DateUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.util.chart.MPChartHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 数据展示
 * Created by Administrator on 2019/4/15 0015.
 */

public class MasterShowStDataActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv_edit)
    ImageView lvEdit;
    @BindView(R.id.tv_start_time)
    TextView tvBeginTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.linechart)
    LineChart linechart;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //开始时间和结束时间
    private String begindate, enddate;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_master_show_st_data;
    }

    @Override
    protected void init() {
        initStateBar();

        //初始化标题
        initTitle();

        //初始时间
        initTime();

        rlLoading.setVisibility(View.GONE);

        //设置曲线图的数据
        initLineData();

    }

    /**
     * 初始化标题
     */
    private void initTitle() {
        tvTitle.setText("数据示意图");
    }


    /**
     * 初始化开始时间和结束时间.默认开始时间为当前月份的前一个
     */
    private void initTime() {
        enddate = DateUtils.getTime(new Date(), "yyyy-MM-dd");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        begindate = format.format(m);

        //设置初始时间
        tvBeginTime.setText(DateUtils.getFmTime(begindate));
        tvEndTime.setText(DateUtils.getFmTime(enddate));
    }


    /**
     * 弹出选择时间的日期选择期
     *
     * @param pos
     */
    private void showTimePicker(final int pos) {
        PickerManager.showPickerByYeaerAndMonth(this, new PickerManager.onSelectedListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                if (pos == 0) {

                    String chooseBeginDate = DateUtils.getTime(date, "yyyy-MM-dd");

                    //比较时间差,然后刷新页面数据
                    long end = DateUtils.getTime(enddate, "yyyy-MM-dd");
                    long start = DateUtils.getTime(chooseBeginDate, "yyyy-MM-dd");

                    //计算选择的开始和结束时间,转化为date格式
                    Date startDate = null;
                    Date endDate = null;
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                    try {
                        startDate = sdf.parse(chooseBeginDate);
                        endDate = sdf.parse(enddate);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    if (end <= start) {
                        ToastUtils.showShort("选择的开始时间不能大于结束时间");
                        return;
                    } else if (DateUtils.getDiffMonth(startDate, endDate) < 1) {
                        //小于1个月
                        ToastUtils.showShort("选择的时间间隔不能小于1个月");
                        return;
                    } else if (DateUtils.getDiffMonth(startDate, endDate) > 6) {
                        //大于6个月
                        ToastUtils.showShort("选择的时间间隔不能超过6个月");
                        return;
                    } else {

                        begindate = DateUtils.getTime(date, "yyyy-MM-dd");

                        tvBeginTime.setText(DateUtils.getFmTime(begindate));

                        //刷新数据
//                        http(BqaManager.PULLREFRESH);
                    }
                } else if (pos == 1) {

                    String chooseEndDate = DateUtils.getTime(date, "yyyy-MM-dd");

                    //比较时间差,然后刷新页面数据
                    long end = DateUtils.getTime(chooseEndDate, "yyyy-MM-dd");
                    long start = DateUtils.getTime(begindate, "yyyy-MM-dd");

                    //计算选择的开始和结束时间,转化为date格式
                    Date startDate = null;
                    Date endDate = null;
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                    try {
                        startDate = sdf.parse(begindate);
                        endDate = sdf.parse(chooseEndDate);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    if (end <= start) {
                        ToastUtils.showShort("选择的结束时间不能小于开始时间");
                    } else if (DateUtils.getDiffMonth(startDate, endDate) < 1) {
                        //小于1个月
                        ToastUtils.showShort("选择的时间间隔不能小于1个月");
                        return;
                    } else if (DateUtils.getDiffMonth(startDate, endDate) > 6) {
                        //大于6个月
                        ToastUtils.showShort("选择的时间间隔不能超过6个月");
                        return;
                    } else {
                        enddate = DateUtils.getTime(date, "yyyy-MM-dd");
                        tvEndTime.setText(DateUtils.getFmTime(enddate));
                        //刷新数据
//                        http(BqaManager.PULLREFRESH);
                    }
                }

            }
        });
    }


    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_back, R.id.tv_start_time, R.id.tv_end_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
            case R.id.tv_start_time:
                showTimePicker(0);
                break;
            case R.id.tv_end_time:
                showTimePicker(1);
                break;
        }
    }


    /**
     * 设置曲线图的数据
     */
    private void initLineData() {

        List<String> xAxisValues = new ArrayList<>();
        xAxisValues.add("老师1");
        xAxisValues.add("老师2");
        xAxisValues.add("老师3");
        xAxisValues.add("老师4");
        xAxisValues.add("老师5");
        xAxisValues.add("老师6");
        xAxisValues.add("老师7");
        xAxisValues.add("老师8");
        xAxisValues.add("老师9");
        xAxisValues.add("老师10");

        List<Float> yAxisValues1 = new ArrayList<>();
        List<Float> yAxisValues2 = new ArrayList<>();
        for (int i = 0; i < 10; ++i) {

            yAxisValues1.add((float) (Math.random() * 20 + 20));
            yAxisValues2.add((float) (Math.random() * 20 + 40));
        }

        List<List<Float>> yAxisValues = new ArrayList<>();
        yAxisValues.add(yAxisValues1);
        yAxisValues.add(yAxisValues2);

        List<String> titles = new ArrayList<>();
        titles.add("");
        titles.add("");

        //颜色
        int[] lineColors = {getResources().getColor(R.color.ms_xz), getResources().getColor(R.color.ms_ls)};

        //设置Y轴的标签数
        MPChartHelper.setLinesChart(linechart, xAxisValues, yAxisValues, titles, false, lineColors);

    }
}
